---
- name: Ubuntu Server Provisioning
  hosts: all
  become: True
  gather_facts: False
  vars:
    ruby_version: 2.6.1
    ruby_download_url: http://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.1.tar.gz
    bundler_version: 1.17.2
    rails_version: 5.1.7
    database_username: dbuser
    database_password: dbpassword
    redis_port: 6379

  tasks:
    - name: install python
      raw: sudo apt update && sudo apt install -y python3 python3-pip python3-setuptools

    - setup:

    - name: install pip modules
      pip:
        name:
          - simplejson

    - name: update apt cache
      apt: update_cache=yes

    - name: upgrade packages
      apt: upgrade=dist

    #
    # Install Postgres
    #

    # https://github.com/geerlingguy/ansible-role-postgresql
    - import_role:
        name: geerlingguy.postgresql
      vars:
        postgresql_service_state: started
        postgresql_service_enabled: true
        postgresql_python_library: python3-psycopg2
        postgresql_users:
          - name: "{{ database_username }}"
            password: "{{ database_password }}"
            role_attr_flags: SUPERUSER

    #
    # Install Redis
    #

    # https://github.com/geerlingguy/ansible-role-redis
    - import_role:
        name: geerlingguy.redis
      vars:
        redis_bind_interface: 127.0.0.1

    #
    # Install Ruby
    #

    # https://github.com/geerlingguy/ansible-role-ruby
    - import_role:
        name: geerlingguy.ruby
      vars:
        workspace: /home/vagrant
        ruby_install_from_source: true
        ruby_install_bundler: false

    - name: install bundler
      shell: "gem install bundler -v {{ bundler_version }}"

    - name: set dir attributes
      file:
        path: "{{ item }}"
        state: directory
        owner: vagrant
        group: vagrant
        mode: 'u+rw'
        recurse: yes
      with_items:
        - /var/lib/gems
        - /usr/local/bin

    #
    # Install Rails
    #

    - name: install curl
      apt:
        name: curl

    - name: install node
      shell: curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
      args:
        warn: no

    - name: add yarn pubkey
      shell: curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
      args:
        warn: no

    - name: update yarn apt respository
      apt_repository:
        repo: deb https://dl.yarnpkg.com/debian/ stable main
        state: present
        filename: yarn

    - name: install extra packages
      apt:
        name: "{{ packages }}"
      vars:
        packages:
        - vim
        - tmux
        - mc
        - git-core
        - net-tools
        - tree
        - zlib1g-dev
        - build-essential
        - libssl-dev
        - libreadline-dev
        - libyaml-dev
        - libsqlite3-dev
        - sqlite3
        - libxml2-dev
        - libxslt1-dev
        - libcurl4-openssl-dev
        - software-properties-common
        - libffi-dev
        - nodejs
        - yarn

    - name: install rails
      gem:
        name: rails
        version: "{{ rails_version }}"
        state: present
      become: False
